/*
Credits go to overSKILL (http://overskill.alexshu.com/saving-loading-settings-on-sd-card-with-arduino/)
The config file muset be in this format:

relay1=0
relay2=1
relay3=0
relay4=2

where 0 is off, 1 is on and anything else is auto
*/

void readSDRelaySettings(){
	char character;
	String settingName;
	String settingValue;
	myFile = SD.open("/settings/relays.ini");
	if (myFile) {
		while (myFile.available()) {
			character = myFile.read();
			while (character != '='){
				settingName = settingName + character;
				character = myFile.read();
			}
			character = myFile.read();
			while (character != '\n'){
				settingValue = settingValue + character;
				character = myFile.read();
				if (character == '\n'){
					/*
					//Debuuging Printing
					Serial.print("Name:");
					Serial.println(settingName);
					Serial.print("Value :");
					Serial.println(settingValue);
					*/
					// Apply the value to the parameter
					applyRelaySetting(settingName, settingValue);
					// Reset Strings
					settingName = "";
					settingValue = "";
				}
			}
		}
		// close the file:
		myFile.close();
	}
	else {
		// if the file didn't open, print an error:
		Serial.println(F("Error opening file!"));
	}
}

// Writes A Configuration file
void writeSDRelaySettings(char* file) {
	// Delete the old One
	SD.remove(file);
	// Create new one
	myFile = SD.open(file, FILE_WRITE);
	// writing in the file works just like regular print()/println() function
	myFile.print(F("relay1="));
	myFile.println(byRelay[0]);
	myFile.print(F("relay2="));
	myFile.println(byRelay[1]);
	myFile.print(F("relay3="));
	myFile.println(byRelay[2]);
	myFile.print(F("relay4="));
	myFile.println(byRelay[3]);
	// close the file:
	myFile.close();
	//Serial.println("Writing done.");
}

/* Apply the value to the parameter by searching for the parameter name */
void applyRelaySetting(String settingName, String settingValue) {
	if (settingName == "relay1") {
		byRelay[0] = settingValue.toInt();
	}
	if (settingName == "relay2") {
		byRelay[1] = settingValue.toInt();
	}
	if (settingName == "relay3") {
		byRelay[2] = settingValue.toInt();
	}
	if (settingName == "relay4") {
		byRelay[3] = settingValue.toInt();
	}
}